import React from "react";
import {Button,  Divider,  Modal} from 'antd/lib';
import NoticeTable from "@/components/notice/NoticeTable";
import Attaches from "@/services/attaches";
import notification from "@/services/notification";
import {get} from "lodash";
import CheckDialog from "@/components/workflow/CheckDialog";
import {currentUser} from "@/services/auth";
import Workflow from "@/services/workflow";
import moment from "moment";
import WarningOutlined from "@ant-design/icons/WarningOutlined";
const { confirm } = Modal;
export default class NoticeToolbar extends React.Component {
  state = {
    loading: true,
    id: 0,//
    //===================
    annex:[],
    annexorgunit:[],
    orgunitid:currentUser.orgunit_id,
  };
  componentWillMount() {
       this.refreshData() ;
  }
  refreshData() {
    let params = {
      sourceid: 0,
      type: "notice",
    }
    Attaches.get(params).then(data => {
      if (data.id > 0) {
        this.setState({
          id: data.id,//
          annex: data.annex,//--操作员ID
         // annexorgunit:data.annexorgunit,
         // orgunitid:data.orgunitid,
          loading: false,
        });
      } else {
        this.setState({
          loading: false,
          id: 0,
          annex: [],//
         // annexorgunit:[],
        //  orgunitid:0,
        });
      }
    }).catch(error => {
        this.setState({
          loading: false,
          id: 0,
          annex: [],//
        //  annexorgunit:[],
        //  orgunitid:0,
        });
      });
}
  ///|| str.match(/^[\s]*$/)
  isEmptyobj = (str) => {
	if(str==='' || str === null || str === 'null' || str === undefined || str === 'undefined'){
		return true;
	}else{
		return false;
	}
}
  handleSave = () => {
    let id=this.state.id;
    let data={id: this.child.state.id,//
            userid: currentUser.id,//--操作员ID
            annex:this.child.state.annex,
          //  annexorgunit:this.child.state.annexorgunit,
            orgunitid:this.child.state.orgunitid,
            type: "notice",
            status:0,
            sourcetype:"notice",
            sourceid:0,
    }
    if(this.child.state.is_public){
       if(!currentUser.isAdmin){
        notification.error("只有系统管理员可以上传公开文档.");
        return;
       }
    }else{
      id=this.child.annexorgunitid;
      data={id: this.child.state.id,//
        userid: currentUser.id,//--操作员ID
        annex:this.child.state.annexorgunit,
      //  annexorgunit:this.child.state.annexorgunit,
        orgunitid:this.child.state.orgunitid,
        type: "orgunit",
        status:0,
        sourcetype:"orgunit",
        sourceid:this.child.state.orgunitid,
      }
    }
    if(id===0) {
      Attaches.create(data)
        .then(() => {
          this.setState({
            loading: false,
            id: data.id,//
            annex: data.annex,//--操作员ID
            annexorgunit: data.annexorgunit,
            orgunitid:data.orgunitid,
          });
          notification.success("保存成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }else{
      Attaches.save(data)
        .then(() => {
          notification.success("保存成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }
  };
  render() {
    if (this.state.loading) return null;
    const data={
      id: this.state.id,//
      annex:this.state.annex,//--纪录事项
      annexorgunit: this.state.annexorgunit,
      orgunitid: this.state.orgunitid,
    };
    return(
      <div>
        <div style={{ marginBottom: 16 }}>
          <Button type="primary" onClick={() => this.handleSave()}>
            <i className="fa fa-save m-r-5" />
            保存
          </Button>
        </div>
        <div>
          <NoticeTable data={data} 
          onRef={(ref) => { this.child = ref; }}/>
        </div>
      </div>
    );
  };
}

